package com.jxm.spring.security.controller;

import com.jxm.spring.security.bean.User;
import com.jxm.spring.security.bean.UsersRoles;
import com.jxm.spring.security.common.ResponseUtils;
import com.jxm.spring.security.service.IUserService;
import com.jxm.spring.security.service.IUsersRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: jxm
 * @Description: 注册用户实例Contrller
 * @Date: 2021/5/16 21:22
 * @Version: 1.0
 */
@RestController
@RequestMapping(value = "/register")
public class RegisterController {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUsersRolesService usersRolesService;


    /**
     * 注册普通用户
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @RequestMapping(value = "/user")
    public ResponseUtils user(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(bCryptPasswordEncoder.encode(password));
        user.setEnabled(0L);
        userService.save(user);

        // 注册普通用户
        UsersRoles usersRoles = new UsersRoles();
        usersRoles.setUserId(user.getUserId());
        usersRoles.setRoleId(2L);
        usersRolesService.save(usersRoles);

        return ResponseUtils.success(user);
    }





}
